//
//  BookViewController.m
//  ZYTS-iPhone
//
//  Created by yuzhou on 12-12-9.
//  Copyright (c) 2012年 wzyk. All rights reserved.
//

#import "ListenInfoViewController.h"
//#import "CommentBookViewController.h"
//#import "AccountViewController.h"
#import "ActivateViewController.h"

@interface ListenInfoViewController ()
{
    ActivateViewController *accountViewCon;
}
@end

@implementation ListenInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotListenInfo:) name:kNotifyGotListenInfo object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readAuthorityFail) name:kReadAuthorityFail object:nil];

        shouldSwipeBack = YES;
    }
    return self;
}
-(void)readAuthorityFail{
    [self showActiveView];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    titleLabel.text = self.listenName;
    contentScrollView.contentSize = CGSizeMake(960, 403);
    if (listenDetailViewController == nil) {
        listenDetailViewController = [[ListenDetailViewController alloc] initWithNibName:@"ListenDetailViewController" bundle:nil];
        listenDetailViewController.listenID = self.listenID;
        listenDetailViewController.view.frame = contentScrollView.bounds;
        [contentScrollView addSubview:listenDetailViewController.view];
    }
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideMenu)];
    [MaskView addGestureRecognizer:tapGesture];
    [tapGesture release];
    
    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(hideMenu)];
    swipeGesture.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft;
    [MaskView addGestureRecognizer:swipeGesture];
    [swipeGesture release];
    
    //阴影效果
    MainView.layer.shadowColor = [UIColor blackColor].CGColor;
    MainView.layer.shadowOpacity = 0.45;
    MainView.layer.shadowOffset = CGSizeMake(0, 6);
    MainView.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 320, 548)].CGPath;
    MainView.layer.masksToBounds = NO;
    
    accountViewCon = [[ActivateViewController alloc]initWithNibName:@"ActivateViewController" bundle:nil];
    [ActiveView addSubview:accountViewCon.view];
}
- (void)hideMenu
{
    [accountViewCon.activateNumTextField resignFirstResponder];
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = MainView.frame;
        rect.origin.x = 0;
        MainView.frame = rect;
        MaskView.alpha = 0;
    }];
    
    [self becomeFirstResponder];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadContentData" object:nil];
}
-(void)showActiveView{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = MainView.frame;
        rect.origin.x = -280;
        MainView.frame = rect;
        
        MaskView.alpha = 0.3;
    }];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // Do any additional setup after loading the view from its nib.
    
    if (listenContentViewController == nil) {
        listenContentViewController = [[ListenContentViewController alloc] initWithNibName:@"ListenContentViewController" bundle:nil];
        listenContentViewController.listenID = self.listenID;
        CGRect rect = contentScrollView.bounds;
        rect.origin.x = 320;
        listenContentViewController.view.frame = rect;
        [contentScrollView addSubview:listenContentViewController.view];
    }
    if (listenCommentViewController == nil) {
        listenCommentViewController = [[ListenCommentViewController alloc] initWithNibName:@"ListenCommentViewController" bundle:nil];
        listenCommentViewController.listenID = self.listenID;
        CGRect rect = contentScrollView.bounds;
        rect.origin.x = 640;
        listenCommentViewController.view.frame = rect;
        [contentScrollView addSubview:listenCommentViewController.view];
    }
   
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    currentPage = contentScrollView.contentOffset.x/320;
    contentPageControl.currentPage = currentPage;
    
    if (currentPage == 2) {
        homeButton.hidden = YES;
//        commentButton.hidden = NO;
    }
    else {
        homeButton.hidden = NO;
//        commentButton.hidden = YES;
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate) {
        [self scrollViewDidEndDecelerating:contentScrollView];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self scrollViewDidEndDecelerating:contentScrollView];
}

- (IBAction)showCommentView:(id)sender
{
//    if ([[AuthorManager defaultManager] hasLoggedIn]) {
//        CommentBookViewController *commentBookViewController = [[[CommentBookViewController alloc] initWithNibName:@"CommentBookViewController" bundle:nil] autorelease];
//        commentBookViewController.itemid = self.itemid;
//        commentBookViewController.itemname = self.itemname;
//        UINavigationController *navController = (UINavigationController *)self.view.window.rootViewController;
//        [navController pushViewController:commentBookViewController animated:YES];
//    } else {
//        AccountViewController *accountViewController = [[[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil] autorelease];
//        accountViewController.needLogin = YES;
//        [self.view.window.rootViewController presentViewController:accountViewController animated:YES completion:nil];
//    }
}

- (IBAction)navBack
{
//    if (currentPage != 0) {
//        [contentScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
//        return;
//    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)gotListenInfo:(NSNotification *)notification
{
    if ([notification.object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *result = [notification.object valueForKey:@"result"];
        if ([result isKindOfClass:[NSDictionary class]]) {
            titleLabel.text = [result objectForKey:@"item_name"];
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [contentScrollView release];
    [_listenID release];
    [_listenName release];
    [listenDetailViewController release];
    [listenContentViewController release];
    [contentPageControl release];
    [titleLabel release];
    [homeButton release];
    [commentButton release];
    [super dealloc];
}
- (void)viewDidUnload {
    [contentScrollView release];
    contentScrollView = nil;
    [contentPageControl release];
    contentPageControl = nil;
    [titleLabel release];
    titleLabel = nil;
    [homeButton release];
    homeButton = nil;
    [commentButton release];
    commentButton = nil;
    [super viewDidUnload];
}
@end
